package com.grm.controller;

import com.grm.document.Article;
import com.grm.repository.ArticleRepository;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Optional;

/**
 * 文章控制器
 *
 * @author gaorimao
 * @date 2022/03/25
 */
@RestController
@RequestMapping("/elk")
public class ArticleController {
    @Resource
    private ArticleRepository articleRepository;

    /**
     * 根据文档id查询数据
     *
     * @param id 文档id
     * @return 文档详情
     */
    @GetMapping("/byId")
    public String findById(@RequestParam String id) {
        Optional<Article> record = articleRepository.findById(id);
        return  record.toString();
    }

    /**
     * 保存文档信息
     *
     * @param article 文档详情
     * @return 保存的文档信息
     */
    @PostMapping("/saveArticle")
    public String saveArticle(@RequestBody Article article) {
        Article result = articleRepository.save(article);
        return result.toString();
    }

    @DeleteMapping("/deleteById")
    public String deleteArticle(@RequestParam String id) {
        articleRepository.deleteById(id);
        return "success";
    }
}

